package servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Category;

import dto.DataManager;

/**
 * Servlet implementation class ViewProductSelected
 */
@WebServlet("/ViewProductSelected")
public class ViewProductSelected extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ViewProductSelected() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");

		HttpSession session = request.getSession();
		
		if (session.getAttribute("userId") == null)
			response.sendRedirect("Message.jsp");

		else {
			DataManager dataManager = new DataManager();
			List<Category> categorys = dataManager
					.getCategorysByParentId(dataManager.getCategoryByName(
							"Linh kiện máy tính").getCategoryId());

			List<Integer> productIds = new ArrayList<Integer>();
			for (int i = 0; i < categorys.size(); ++i) {
				String categoryName = request.getParameter(categorys.get(i)
						.getCategoryName());
				if (categoryName != null) {
					int productId = Integer.valueOf(request
							.getParameter(categorys.get(i).getCategoryName()));
					if (productId != -1)
						productIds.add(productId);
				}
			}

			session.setAttribute("productIds", productIds);
			response.sendRedirect("ReviewComponent.jsp");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
